package com.xunji.standard.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * @description: 增加分页功能
 * @author: xuqiang_imyours
 * @date: 2023/7/5 15:41
 */
@Configuration
public class MybatisPlusConfig {

    @Value("${spring.datasource.driverClassName:}")
    private String datasourceName;

    /**
     * 分页插件。如果你不配置，分页插件将不生效
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 指定数据库方言为 MYSQL
        if (StringUtils.hasText(datasourceName) && datasourceName.contains("mysql")) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        }

        // 指定数据库方言为人大金仓 KINGBASE8
        if (StringUtils.hasText(datasourceName) && datasourceName.contains("kingbase8")) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.KINGBASE_ES));
        }

        return interceptor;
    }
}
